{extend name="common/block" /}

{block name="content"}

<link rel="stylesheet" href="/static/js/jconfirm/jquery-confirm.min.css">
<link href="/static/css/style.min.css" rel="stylesheet">
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-toolbar clearfix">
                <div class="toolbar-btn-action">
                    <a class="btn btn-success m-r-5" href="javascript:void(0);" onclick="start(null,this)"><i
                            class="mdi mdi-check"></i> 启用</a>
                    <a class="btn btn-warning m-r-5" href="javascript:void(0);" onclick="forbiden(null,this)"><i
                            class="mdi mdi-block-helper"></i> 禁用</a>
                    <a class="btn btn-danger" href="javascript:;" onclick="del(null,this)"><i
                            class="mdi mdi-window-close"></i> 删除</a>
                </div>
            </div>
            <div class="card-body">

                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>
                                <label class="lyear-checkbox checkbox-primary">
                                    <input type="checkbox" id="check-all"><span></span>
                                </label>
                            </th>
                            <th>短网址</th>
                            <th style="width:25%;">长网址</th>
                            <th>访问数量</th>
                            <th>链接类型</th>
                            <th>状态</th>
                            <th>创建时间</th>
                            <th>过期时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {volist name="list" id="vo" }
                        <tr>
                            <td>
                                <label class="lyear-checkbox checkbox-primary">
                                    {if $vo.status == 2}
                                    <input type="checkbox" name="ids[]" value="{$vo.id}" status="{$vo.status}" disabled><span></span>
                                    {else /}
                                    <input type="checkbox" name="ids[]" value="{$vo.id}"
                                           status="{$vo.status}"><span></span>
                                    {/if}
                                </label>
                            </td>
                            <td><a href="/s/{$vo.short_code}" target="_blank">{$Request.domain}/s/{$vo.short_code}</a>
                            </td>
                            <td><a href="{$vo.url}" target="_blank">{if strlen($vo.url) > 80}{$vo.url|substr=0,80}...{else/}{$vo.url}{/if}</a></td>
                            <td><a href="/user/visit/info?id={$vo.id}" title data-toggle="tooltip" data-original-title="短链接【{$vo.short_code}】的详细信息">{$vo.visit}</a></td>
                            <td><font class="{$vo.scene_css}">{$vo.scene_text}</font></td>
                            <td><font class="{$vo.status_css}">{$vo.status_text}</font></td>
                            <td>{$vo.create_time}</td>
                            <td>{$vo.format_expire_time}</td>
                            <td>
                                <div class="btn-group">
                                    {if $vo.status == 1}
                                    <a
                                            class="btn btn-xs btn-warning"
                                            href="javascript:;"
                                            title="禁用"
                                            data-toggle="tooltip"
                                            onclick="forbiden('{$vo.id}',this)"
                                    >禁用</a>
                                    {elseif $vo.status == 0 /}
                                    <a
                                            class="btn btn-xs btn-success"
                                            href="javascript:;"
                                            title="启用"
                                            data-toggle="tooltip"
                                            onclick="normal('{$vo.id}',this)"
                                    >启用</a>
                                    {/if}
                               <!--     <a
                                            href="{:url('visit/info',['id'=>$vo.id])}"
                                            class="btn btn-xs btn-info"
                                    >
                                        详细信息
                                    </a>-->
                                    <a
                                            class="btn btn-xs btn-danger"
                                            href="javascript:;"
                                            title="删除"
                                            data-toggle="tooltip"
                                            onclick="del('{$vo.id}',this)"
                                    >删除</a>
                                </div>
                            </td>
                        </tr>
                        {/volist}
                        </tbody>
                    </table>
                </div>
                {if $count > $limit}
                <ul class="pagination">
                    {if $pages <= 0}
                    <li class="disabled"><span>«</span></li>
                    {else/}
                    <li><a href="{:url('',['pages'=>$pages-1])}">«</a></li>
                    {/if}
                    {for start="1" end="$end+1" step=1 name="i"}
                    {if $i == $pages}
                    <li class="active"><span>{$i}</span></li>
                    {else / }
                    <li><a href="{:url('',['pages'=>$i])}">{$i}</a></li>
                    {/if}
                    {/for}
                    {if $pages+1 > $count}
                    <li class="disabled"><span>»</span></li>
                    {else/}
                    <li><a href="{:url('',['pages'=>$pages+1])}">»</a></li>
                    {/if}
                </ul>
                {/if}

            </div>
        </div>
    </div>
</div>
<div id="modal" class="modal fade" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">x</button>
                <h3>对话框标题</h3>
            </div>
            <div class="modal-body">
                <p>对话框主体</p>
            </div>
            <div class="modal-footer">
                <a href="#" class="btn" data-dismiss="modal">取消</a>
                <a href="#" class="btn btn-primary" data-dismiss="modal">确定</a>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="jsLogic"}
<script type="text/javascript" src="/static/js/bootstrap-notify.min.js"></script>
<script src="/static/js/jconfirm/jquery-confirm.min.js"></script>
<script type="text/javascript" src="/static/js/lightyear.js"></script>
<script type="text/javascript" src="/static/js/main.min.js"></script>
<script>
    function del(id, e) {
        var ids = new Array();
        if (id == null) {
            $('table tbody input[type="checkbox"]').each(function () {
                if ($(this).is(':checked')) {
                    ids.push($(this).val());
                }
            })
        } else {
            ids.push(id);
        }

        if (ids.length <= 0) {
            lightyear.notify('请选择要操作的数据', 'warning');
            return;
        }
        $.confirm({
            title: '警告',
            content: '确认要删除该数据？',
            type: 'red',
            typeAnimated: true,
            buttons: {
                tryAgain: {
                    text: '确认',
                    btnClass: 'btn-red',
                    action: function () {
                        $.ajax({
                            url: 'operaLinks',
                            method: 'post',
                            data: {
                                ids: ids,
                                type: 'del',
                            },
                            success: function (res) {
                                if (res.code == 200) {
                                    lightyear.notify('数据已成功删除', 'success', 1000, '', 'top', 'center')
                                    setTimeout(function () {
                                        location.reload();

                                    }, 1500);
                                } else {
                                    lightyear.notify(res.message, 'warning', 1000, '', 'top', 'center')
                                }
                            },
                            error: function (res) {
                                console.log(res);
                                lightyear.notify('未知错误', 'danger', 1000, '', 'top', 'center')
                            }
                        })
                    }
                },
                close: {
                    text: '关闭'
                }
            }
        });
    }

    function normal(id, e) {
        var ids = new Array();
        if (id == null) {
            $('table tbody input[type="checkbox"]').each(function () {
                if ($(this).is(':checked')) {
                    if ($(this).attr('status') == 2) {
                        lightyear.notify('已过期的数据不能进行该操作', 'warning');
                        return;
                    }
                    ids.push($(this).val());
                }
            })
        } else {
            ids.push(id);
        }

        if (ids.length <= 0) {
            lightyear.notify('请选择要操作的数据', 'warning');
            return;
        }
        $.ajax({
            url: 'operaLinks',
            method: 'post',
            data: {
                ids: ids,
                type: 'open',
            },
            success: function (res) {
                if (res.code == 200) {
                    lightyear.notify(res.message, 'success', 1000, '', 'top', 'center')
                    setTimeout(function () {
                        location.reload();
                    }, 1500);
                } else {
                    lightyear.notify(res.message, 'warning', 1000, '', 'top', 'center')
                }
            },
            error: function (res) {
                console.log(res);
                lightyear.notify('未知错误', 'danger', 1000, '', 'top', 'center')
            }
        })
    }

    function forbiden(id, e) {
        var ids = new Array();
        if (id == null) {
            $('table tbody input[type="checkbox"]').each(function () {
                if ($(this).is(':checked')) {
                    if ($(this).attr('status') == 2) {
                        lightyear.notify('已过期的数据不能进行该操作', 'warning');
                        return;
                    }
                    ids.push($(this).val());
                }
            })
        } else {
            ids.push(id);
        }

        if (ids.length <= 0) {
            lightyear.notify('请选择要操作的数据', 'warning');
            return;
        }
        $.ajax({
            url: 'operaLinks',
            method: 'post',
            data: {
                ids: ids,
                type: 'forbiden',
            },
            success: function (res) {
                if (res.code == 200) {
                    lightyear.notify(res.message, 'success', 1000, '', 'top', 'center')
                    setTimeout(function () {
                        location.reload();

                    }, 1500);
                } else {
                    lightyear.notify(res.message, 'warning', 1000, '', 'top', 'center')
                }
            },
            error: function (res) {
                console.log(res);
                lightyear.notify('未知错误', 'danger', 1000, '', 'top', 'center')
            }
        })
    }
</script>
{/block}
